<?php
require_once 'ApibaseController.php';

/**
 * 法人問い合わせAPI<br />
 *
 * 法人問い合わせAPI
 *
 * @package apamanshop
 * @author ushijima
 * @copyright コピーライト(要確認)
 */
class Inquiry_ApicorporateController extends Inquiry_ApibaseController
{
	/**
	 * 各種問合せAPIアクション
	 *
	 * @access public
	 * @return void
	 */
	public function indexAction()
	{

		// 変数初期化処理 start

		$sCompany = $this->_params['Company'];
		$sZip1 = $this->_params['Zip1'];
		$sZip2 = $this->_params['Zip2'];
		$sPrefCd = $this->_params['PrefCd'];
		$sAddress1 = $this->_params['Address1'];
		$sAddress2 = $this->_params['Address2'];
		$sBusho = $this->_params['Busho'];
		$sYakushoku = $this->_params['Yakushoku'];
		$sTantoushaName = $this->_params['ChargeNm'];
		$sTantoushaKana = $this->_params['ChargeNmKana'];
		$sTel = $this->_params['Tel'];
		$sFax = $this->_params['Fax'];
		$sMail = $this->_params['Mail'];
		$sUrl = $this->_params['Url'];
		$sComment = $this->_params['Comment'];

		//送信時刻
		$strTimeStr = $this->_params['TimeStr'];

		$add_params['Toiawase'] == "";
		if($this->_params['Inquiry1'] != "") {
			$add_params['Toiawase'] = $this->_params['Inquiry1'] ."\r\n";
			$this->_params['Inquiry1'] = "1";
		}else{
			$this->_params['Inquiry1'] = "0";
		}
		if($this->_params['Inquiry2'] != "") {
			$add_params['Toiawase'] .= $this->_params['Inquiry2'] ."\r\n";
			$this->_params['Inquiry2'] = "1";
		}else{
			$this->_params['Inquiry2'] = "0";
		}
		if($this->_params['Inquiry3'] != "") {
			$add_params['Toiawase'] .= $this->_params['Inquiry3'] ."\r\n";
			$this->_params['Inquiry3'] = "1";
		}else{
			$this->_params['Inquiry3'] = "0";
		}

		//--- メール判定 ---
		$this->setSendMailFlag($this->_params['Mail']);

		// ﾛｸﾞ情報格納処理 *****************************************
		try {
			// モデルの呼び出し
			$this->_model = new Inquiry_Model_Apiinquiry();

			//トランザクション開始
			$db = $this->_model->getDbConnection();
			$this->_model->startTransaction($db);

			$list_data = $this->_model->getInqPrefQry($db,$this->_params);
			$this->_params['PrefName'] = $list_data['M43_TODOUFUKEN'];

			if ($this->isLogIns()) {
				//対応状況TBLﾃﾞｰﾀ挿入処理
				$resApi = $this->_model->insCompanyToiawaseLog($db,$this->_params,$add_params);
			}

			// ﾒｰﾙ送信処理 ********************************************
			$result =  $this->SendMailMain($sMail,$add_params);
			if($result == "0"){
				 $this->raiseError('メール送信エラー');
			}

			__log_debug(__FILE__, __LINE__, __METHOD__, "コミットします");
			$this->_model->commitTransaction($db);
			$db->closeConnection();

			$result = 1;

		} catch (Exception $e) {
			__log_debug(__FILE__, __LINE__, __METHOD__, "ロールバックします");
			$this->_model->rollbackTransaction($db);
			$db->closeConnection();
			$result = 0;
		}

		// 返却データ作成
		$resResult = array('api_result' => array('result' => $result,'res_detail' => $resApi));
		// 返却処理
		$this->_api_data = $resResult;
		$this->display();

	}

	//メール送信制御
	private function SendMailMain($mail,$addinfo) {
		// ｽﾃｰﾀｽ初期化

		// ユーザへメール送信
		if ($mail != "") {
			$userResult = $this->SendMailUser($mail,$addinfo);
		}

		// 本部へのメール送信
		$officeResult = $this->SendMailOffice($addinfo);

		$result = 1;
		if ( $userResult == 0 || $officeResult == 0) {
			$result = 0;
		}
		return $result;
	}

	// 問い合わせユーザへのメール送信
	private function SendMailUser($mail,$addinfo) {

		// from(送信元)、to（あて先)
		$strMailTo = array($mail => "");						//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array("as-h@apamanshop.co.jp" => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ

		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] =  "アパマンショップお問い合わせ確認";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		// ﾒｰﾙ送信処理
		if($this->isSendUser() == true) {
			$result = $this->sendToiawaseMail("MailCorporateUser.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}
		return $result;
	}

	// 本部のメール送信
	private function SendMailOffice($addinfo) {

		$strSubject = "法人社宅斡旋ページからのお問合わせ";
		$strMailTo =   array("as-h@apamanshop.co.jp" => "");	//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array("as-h@apamanshop.co.jp" => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = $strSubject;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if($this->isSendHonbu() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailCorporateHonbu.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceHonbuMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailCorporateHonbu.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}
}
?>
